package com.lrkj.payfee.service.impl;

import java.util.List;

import com.lrkj.common.constant.OrgConstants;
import com.lrkj.common.core.domain.entity.OrgTree;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.property.domain.Community;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lrkj.payfee.mapper.MeterBoxMapper;
import com.lrkj.payfee.domain.MeterBox;
import com.lrkj.payfee.service.IMeterBoxService;

/**
 * 仪表箱Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-27
 */
@Service
public class MeterBoxServiceImpl implements IMeterBoxService
{
    @Autowired
    private MeterBoxMapper meterBoxMapper;

    @Autowired
    private IOrgTreeService orgTreeService;

    /**
     * 查询仪表箱
     *
     * @param boxId 仪表箱ID
     * @return 仪表箱
     */
    @Override
    public MeterBox selectMeterBoxById(String boxId)
    {
        return meterBoxMapper.selectMeterBoxById(boxId);
    }

    /**
     * 查询仪表箱列表
     *
     * @param meterBox 仪表箱
     * @return 仪表箱
     */
    @Override
    public List<MeterBox> selectMeterBoxList(MeterBox meterBox)
    {
        return meterBoxMapper.selectMeterBoxList(meterBox);
    }

    /**
     * 新增仪表箱
     *
     * @param meterBox 仪表箱
     * @return 结果
     */
    @Override
    public int insertMeterBox(MeterBox meterBox)
    {
        meterBox.setBoxId(IdUtils.simpleUUID());
        meterBox.setCreateBy(SecurityUtils.getUsername());
        meterBox.setCreateTime(DateUtils.getNowDate());
        this.setUnitInfo(meterBox);
        return meterBoxMapper.insertMeterBox(meterBox);
    }

    /**
     * 修改仪表箱
     *
     * @param meterBox 仪表箱
     * @return 结果
     */
    @Override
    public int updateMeterBox(MeterBox meterBox)
    {
        this.setUnitInfo(meterBox);
        meterBox.setUpdateBy(SecurityUtils.getUsername());
        meterBox.setUpdateTime(DateUtils.getNowDate());
        return meterBoxMapper.updateMeterBox(meterBox);
    }

    /**
     * 批量删除仪表箱
     *
     * @param boxIds 需要删除的仪表箱ID
     * @return 结果
     */
    @Override
    public int deleteMeterBoxByIds(String[] boxIds)
    {
        orgTreeService.deleteOrgTreeByIds(boxIds);
        return meterBoxMapper.deleteMeterBoxByIds(boxIds);
    }

    /**
     * 删除仪表箱信息
     *
     * @param boxId 仪表箱ID
     * @return 结果
     */
    @Override
    public int deleteMeterBoxById(String boxId)
    {
        orgTreeService.deleteOrgTreeById(boxId);
        return meterBoxMapper.deleteMeterBoxById(boxId);
    }


    private void setUnitInfo(MeterBox bean) {
        OrgTree parentBean = orgTreeService.selectOrgTreeById(bean.getDeptId());
        bean.setAncestors(parentBean.getAncestors() + "," + bean.getDeptId());
        orgTreeService.insertOrgTree(bean.getBoxId(), bean.getBoxName(), bean.getDeptId(), OrgConstants.box, bean.getAncestors());
    }

}
